import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  // 定价模型数据
  const pricingModels = [
    {
      id: '1',
      name: '酒店客房固定定价',
      type: 'fixed',
      description: '酒店客房固定价格定价模型',
      customerCurrency: 'CNY',
      customerBasePrice: 500,
      customerUnit: 'night',
      platformCost: 200,
      platformMargin: 0.2,
      platformRevenueShare: 0.3,
      status: 'active',
      createdAt: '2024-01-10T09:00:00Z',
      updatedAt: '2024-01-15T14:30:00Z',
      rules: [
        {
          id: '1',
          name: '旺季加价',
          type: 'seasonal',
          enabled: true,
          conditions: {
            season: 'peak',
            multiplier: 1.5,
          },
        },
        {
          id: '2',
          name: '长期住宿优惠',
          type: 'duration',
          enabled: true,
          conditions: {
            minDays: 7,
            discount: 0.1,
          },
        },
      ],
    },
    {
      id: '2',
      name: '温泉服务按量计费',
      type: 'usage',
      description: '温泉服务按使用量计费模型',
      customerCurrency: 'CNY',
      customerBasePrice: 100,
      customerUnit: 'hour',
      platformCost: 40,
      platformMargin: 0.3,
      platformRevenueShare: 0.25,
      status: 'active',
      createdAt: '2024-01-12T11:20:00Z',
      updatedAt: '2024-01-14T16:45:00Z',
      rules: [
        {
          id: '3',
          name: '包月优惠',
          type: 'subscription',
          enabled: true,
          conditions: {
            monthlyHours: 80,
            discount: 0.2,
          },
        },
      ],
    },
    {
      id: '3',
      name: '景区门票订阅定价',
      type: 'subscription',
      description: '景区门票订阅制定价模型',
      customerCurrency: 'CNY',
      customerBasePrice: 200,
      customerUnit: 'month',
      platformCost: 80,
      platformMargin: 0.25,
      platformRevenueShare: 0.2,
      status: 'active',
      createdAt: '2024-01-08T15:10:00Z',
      updatedAt: '2024-01-13T10:20:00Z',
      rules: [
        {
          id: '4',
          name: '年费优惠',
          type: 'annual',
          enabled: true,
          conditions: {
            annualDiscount: 0.15,
          },
        },
        {
          id: '5',
          name: '家庭套餐',
          type: 'family',
          enabled: true,
          conditions: {
            familyMembers: 4,
            discount: 0.25,
          },
        },
      ],
    },
    {
      id: '4',
      name: '餐饮服务混合定价',
      type: 'hybrid',
      description: '餐饮服务固定+按量混合定价模型',
      customerCurrency: 'CNY',
      customerBasePrice: 50,
      customerUnit: 'meal',
      platformCost: 20,
      platformMargin: 0.35,
      platformRevenueShare: 0.28,
      status: 'draft',
      createdAt: '2024-01-15T09:30:00Z',
      updatedAt: '2024-01-15T09:30:00Z',
      rules: [
        {
          id: '6',
          name: '团餐优惠',
          type: 'group',
          enabled: true,
          conditions: {
            minPeople: 10,
            discount: 0.1,
          },
        },
      ],
    },
    {
      id: '5',
      name: '医美服务阶梯定价',
      type: 'tiered',
      description: '医美服务阶梯式定价模型',
      customerCurrency: 'CNY',
      customerBasePrice: 1000,
      customerUnit: 'session',
      platformCost: 400,
      platformMargin: 0.4,
      platformRevenueShare: 0.35,
      status: 'active',
      createdAt: '2024-01-06T13:45:00Z',
      updatedAt: '2024-01-12T17:30:00Z',
      rules: [
        {
          id: '7',
          name: '疗程优惠',
          type: 'package',
          enabled: true,
          conditions: {
            sessions: 6,
            discount: 0.2,
          },
        },
        {
          id: '8',
          name: 'VIP会员价',
          type: 'vip',
          enabled: true,
          conditions: {
            vipLevel: 'gold',
            discount: 0.15,
          },
        },
      ],
    },
  ];

  // 过滤逻辑
  let filteredModels = pricingModels;

  if (query.keyword) {
    const keyword = query.keyword as string;
    filteredModels = filteredModels.filter(
      (model) =>
        model.name.includes(keyword) || model.description.includes(keyword),
    );
  }

  if (query.type) {
    filteredModels = filteredModels.filter(
      (model) => model.type === query.type,
    );
  }

  if (query.status) {
    filteredModels = filteredModels.filter(
      (model) => model.status === query.status,
    );
  }

  // 分页
  const page = parseInt(query.page as string) || 1;
  const pageSize = parseInt(query.pageSize as string) || 10;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const items = filteredModels.slice(start, end);

  return {
    code: 0,
    data: {
      items,
      total: filteredModels.length,
      page,
      pageSize,
    },
    msg: '获取定价模型列表成功',
  };
});

